import json

import requests

from faker import Faker

fake = Faker('zh_CN')

indexFileData = 0  # 当前读到第几个文件
fileNamePrefix = '非自动爬取知乎穿搭'   #知乎穿搭
foodStoreData = []  # 读取一个文件所得到的foodData数据
urlPostSave = 'http://127.0.0.1:8081/api/front/community/post/save'
urlCommentSave = 'http://127.0.0.1:8080/api/admin/store/product/reply/save'
# headers = {'Authorization': PEXELS_API_KEY}
headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.188 Safari/537.36 CrKey/1.54.250320',
    'Authori-Zation': 'd8bed6776ecb49fdb6a734c5cc86653e',
    'Content-Type': 'application/json'
}
for file_index in range(1, 6):  # 第一个索引值，看上次你读取到哪里的索引-------------- 6
    # 加载生成的食品数据
    print(f"加载{fileNamePrefix}{file_index}文件")
    with open(f'../postJsonData/{fileNamePrefix}{file_index}.json', 'r', encoding='UTF-8') as f:
        foodStoreData = json.load(f)
        indexFileData=file_index
    while len(foodStoreData) > 0:
        storeProduct = foodStoreData.pop()  # pop一个data数据进行插入
        # 将JSON数据转换为字符串
        storeProduct = json.dumps(storeProduct)
        # 插入Item-------------------
        response = requests.post(urlPostSave, data=storeProduct, headers=headers)
        if response.status_code == 200:
            data = response.json()
            # print(data)
            if data['code'] == 200:
                print(f"插入post成功")
            else:
                print(f"插入post失败，code!=200")
        else:
            print(f"插入post失败，系统无法响应")
    # 插入Item-------------------

print('++++++++++++++++++++++++++++++++')
print(f'当前读取到文件索引{indexFileData}')
re = {
    '数据文件前缀': fileNamePrefix,
    '当前读取到文件索引': indexFileData
}
# 数据库出错了，
with open('./re/insetResult_' + fileNamePrefix + '.json', 'w', encoding='utf-8') as f:
    json.dump(re, f, ensure_ascii=False, indent=4)


# 它大爷的 后面发现其实它的电影ID就是1-1682 所以干脆直接插入电影就好了，，压根不需要读取交互文件  操他吗的、日 浪费老子电脑何时间
